package com.atguigu.petstore.sys.controller;

import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.sys.entity.SysUser;
import com.atguigu.petstore.sys.service.SysUserService;
import com.atguigu.petstore.sys.vo.SysUserHeaderImgUpdateVo;
import com.atguigu.petstore.sys.vo.SysUserSearchParamVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-09-21 13:55
 * @RequestMapping("/api/sys")：命名规则
 *    url：     /api/模块/模型
 *    method：
 *              GET：查询
 *                  查所有： /api/sys/user[?k=v&k=v]
 *                  查单个： /api/sys/user/{id}
 *              POST：新增
 *                  批量：  /api/sys/user   数据放到请求体，是json方式; [{},{}]
 *                  单个：  /api/sys/user   数据放到请求体，是json方式; {}
 *              PUT：修改
 *              DELETE：删除； 不能提供批量删除
 */
@RequestMapping("/api/sys")
@RestController
@Tag(name = "系统用户管理")
public class SysUserController {


    @Autowired
    SysUserService sysUserService;

    //每页大小
    final Long ps = 10L;

    /**
     * 查询所有 用户
     * @return
     */
    @Operation(summary = "查询所有系统用户")
    @GetMapping("/user")
    public Result getAllUser(){
        //业务
        List<SysUser> sysUsers = sysUserService.list();

        //返回
        return Result.ok(sysUsers);
    }


    /**
     * 分页查询用户列表
     * @param pn
     * @param paramVo  vo中的所有属性。默认从 请求参数中获取
     * @return
     *
     *   @RequestBody： Post请求体
     */
    @Operation(summary = "分页查询")
    @GetMapping("/user/page/{pn}")
    public Result getUserPage(@PathVariable("pn") Long pn,
                              SysUserSearchParamVo paramVo){


        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();

        if(StringUtils.hasText(paramVo.getUsername())){
            wrapper.like(SysUser::getLoginName,paramVo.getUsername());
        }

        if(StringUtils.hasText(paramVo.getEmail())){
            wrapper.like(SysUser::getSafeEmail,paramVo.getEmail());
        }

        //1、分页查询
        PageDTO<SysUser> page = sysUserService.page(new PageDTO<SysUser>(pn,ps),wrapper);


        return Result.ok(page);
    }

    @Operation(summary = "获取总页码")
    @GetMapping("/user/page/total")
    public Result getTotalPages(){
        long total = sysUserService.count();

        //页码计算：

        return Result.ok(total%ps == 0? total/ps : (total/ps + 1));
    }


    @Operation(summary = "按照id删除")
    @DeleteMapping("/user/{id}")
    public Result deleteById(@PathVariable("id") Long id){

        sysUserService.removeById(id);

        return Result.ok();
    }


    @Operation(summary = "用户更新")
    @PutMapping("/user")
    public Result updateUser(@RequestBody SysUser sysUser){

        //按照id，把其他字段都更新了
        sysUserService.updateUser(sysUser);
        return Result.ok();
    }


    @Operation(summary = "新增用户")
    @PostMapping("/user")
    public Result addUser(@RequestBody SysUser sysUser){
        sysUserService.addUser(sysUser);

        return Result.ok();
    }


    @Operation(summary = "按照id查询用户")
    @GetMapping("/user/{id}")
    public Result getById(@PathVariable("id") Long id){
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }


    @Operation(summary = "修改头像")
    @PutMapping("/user/headerimg")
    public Result updateHeaderImage(@RequestBody SysUserHeaderImgUpdateVo vo,
                                    @RequestHeader("token") String token){


        sysUserService.updateHeaderImg(token,vo);


        return Result.ok();
    }

}
